package org.umss.fixture.services;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.umss.fixture.model.Championship;
import org.umss.fixture.model.ChampionshipList;
import org.umss.fixture.model.PhaseList;
import org.umss.fixture.model.TeamList;
import org.umss.fixture.model.TeamPosition;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class FixtureBuilderServices {

    private static final String SERVER_URL = "http://testing.fixture-builder.appspot.com/resources";
    private Connection connection = new Connection(SERVER_URL);

    public List<String> getAvailableChampionships() {
        List<String> championshipList = new ArrayList<String>();
        try {
            String json = connection.get("/championship/all");
            Log.w("JSON CHAMP", json);
            if (json.contains("[")) {
                ChampionshipList list = new ChampionshipList();
                Gson gson = new Gson();
                Type type = new TypeToken<ChampionshipList>() {
                }.getType();
                list = gson.fromJson(json, type);
                championshipList = list.getChampionships();
            } else {
                JSONObject jsona = new JSONObject(json);
                String championship = jsona.getString("championships");
                championshipList.add(championship);
            }
        } catch (ConnectionException e) {
            Log.w("ConnectionException", e.getMessage());
        } catch (JSONException e) {
            Log.w("JSONException", e.getMessage());
        }

        return championshipList;
    }

    public PhaseList getAvailablePhases(String championshipName) {
        PhaseList phases = new PhaseList();
        try {
            String json = connection.get("/phase/all/" + championshipName);
            Log.w("JSON CHAMP", json);
            if (!json.equals("null")) {
                Gson gson = new Gson();
                Type type = new TypeToken<PhaseList>() {
                }.getType();
                phases = gson.fromJson(json, type);
            }
        } catch (ConnectionException e) {
            Log.w("ConnectionException", e.getMessage());
        }

        return phases;
    }

    public void saveChampionship(Championship newChampionship) {
        try {
            JSONObject object = new JSONObject();
            object.put("name", newChampionship.getName());
            JSONArray teams = new JSONArray();
            for (int i = 1; i <= newChampionship.getTeamsNumber(); i++) {
                teams.put("Team - " + i);
            }
            object.put("teams", teams);

            connection.postPayload("/championship", object.toString());

        } catch (JSONException e) {
            Log.w("JSONException", e.getMessage());
        }
    }

    public void savePhase(String championshipName) {
        try {
            JSONObject object = new JSONObject();
            object.put("championshipId", championshipName);
            object.put("type", 0);
            connection.postPayload("/phase", object.toString());
        } catch (JSONException e) {
            Log.w("JSONException", e.getMessage());
        }

    }

    public List<TeamPosition> getScoreBoard(String championshipName) {
        TeamList phases = new TeamList();
        try {
            String json = connection.get("/results/" + championshipName);
            Log.w("JSON CHAMP", json);
            if (!json.equals("null")) {
                Gson gson = new Gson();
                Type type = new TypeToken<TeamList>() {
                }.getType();
                phases = gson.fromJson(json, type);
            }
        } catch (ConnectionException e) {
            Log.w("ConnectionException", e.getMessage());
        }

        return phases.getPositions();
    }

    public void saveResult(String id, int localScore, int visitorScore) {

        // String url = "/result/" + id + "?local_score=" + localScore
        // +"&visitor_score="+ visitorScore;

        // connection.post("", postParameters);
    }
}
